package com.example.villagedevelop.generator.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.villagedevelop.generator.entity.*;
import com.example.villagedevelop.generator.mapper.HomeStayMapper;
import com.example.villagedevelop.generator.mapper.ImageHomeStayMapper;
import com.example.villagedevelop.generator.service.HomeStayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;

@RestController
@RequestMapping("/home")
public class HomeStayController {

    @Autowired
    HomeStayService homeStayService;
    @Autowired
    ImageHomeStayMapper imageHomeStayMapper;
    @Autowired
    HomeStayMapper mapper;

    @GetMapping("/visit")
    public List<HomeStayVO> findAll(){
        return homeStayService.findAll();
    }//返回所有数据


    @GetMapping("/visitTown")//返回Town
    public List<String> findTown(){
        List<HomeStay> homeStayList = homeStayService.list();
        List<String> towns = new ArrayList<>();

        //去重
        for (HomeStay homeStay:homeStayList){
            towns.add(homeStay.getTown());
        }
        LinkedHashSet<String> hashSet = new LinkedHashSet<>(towns);
        ArrayList<String> townList = new ArrayList<>(hashSet);
        return townList;
    }
    @GetMapping("/select/{town}")//根据town返回village
    public List<String> selectByTown(@PathVariable("town") String town){
        return homeStayService.findVillage(town);
    }

    @GetMapping("/select/{town}/1")//根据town返回HomeStayVO
    public List<HomeStayVO> selectByTown_village(@PathVariable("town") String town){
        return homeStayService.findHomeStayVOByTown(town);
    }

    @GetMapping("/select/{town}/{village}")//根据town和village返回HomeStayVO
    public List<HomeStayVO> selectByTown_village(@PathVariable("town") String town,@PathVariable("village") String village){
        return homeStayService.findHomeStayVOByTown_village(town,village);
    }

    @GetMapping("/select/{field}/{min}/{max}")//范围查找
    public List<HomeStayVO> select_Classify(@PathVariable("field") String field,@PathVariable("min") int min,@PathVariable("max") int max){
        return homeStayService.findHomeStayVO_Classify(field,min,max);
    }


    @GetMapping("/deleteImage")//删除image
    public void delete(@RequestParam("image") String image){
        QueryWrapper<ImageHomeStay> wrapper = new QueryWrapper<>();
        wrapper.eq("image_file",image);
        imageHomeStayMapper.delete(wrapper);
    }
    @GetMapping("/deleteObject")//删除数据
    public void delete(int id){
        homeStayService.deleteObject(id);
    }

    @PostMapping("/saveData")//差诶数据
    public int insertData(@RequestBody HomeStay homeStay){
        homeStayService.save(homeStay);
        homeStay.setDefaultSort(homeStay.getId());//设置默认排序与id一样
        homeStayService.updateById(homeStay);
        return homeStay.getId();
    }
    @PostMapping("/saveImages")//添加image
    public String upload(int id, MultipartFile img){//request用于获取服务器绝对路径
        if (img.getSize()>0){//判断文件是否为空
            //获取保存文件file路径，将文件保存到tomcat服务器的文件目录下，也可以自己写文件路
//tomcat服务器路径：D:\mytool\apache-tomcat-8.5.65-windows-x64\apache-tomcat-8.5.65\webapps\ROOT
            String path = "D:/picture/image";
            //获取上传的文件名
            String name = img.getOriginalFilename();
            File file = new File(path,name);

            String suffixName = name.substring(name.lastIndexOf("."));
            List<String> extList = Arrays.asList(".jpg", ".JPG",".png", ".jpeg", ".gif");
            if (!extList.contains(suffixName)) {
                return "图片格式非法";
            }

            try {
                img.transferTo(file);//将img保存进file文件
                ImageHomeStay imageHomeStay = new ImageHomeStay();
                String name_random = String.valueOf(System.currentTimeMillis());
                name_random += (int)((Math.random() * 9 + 1)* 10000);
                imageHomeStay.setImageFile("http://zbnczy.com/image/"+name_random+suffixName);
                imageHomeStay.setSId(id);
                imageHomeStayMapper.insert(imageHomeStay);

                //保存上传之后的文件路径，
//                request.setAttribute("path","/file/"+name);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "ok!";
    }

    @GetMapping("/changeSort")//改变默认排序
    public void changeDefaultSort(@RequestParam("id1") int id1,@RequestParam("id2") int id2){
        homeStayService.changeDefaultSort(id1, id2);
    }

    //修改信息
    @PostMapping("modify")
    public void modify(@RequestBody HomeStay homeStay){
        homeStayService.updateById(homeStay);
    }

    //模糊查询
    @GetMapping("/search")
    public List<House> search(String word){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("title",word);
        return mapper.selectList(wrapper);
    }


}
